<template>
	<view class="container">
		<uni-segmented-control :current="currentIndex" :values="items" style-type="text" active-color="#007aff" @clickItem="onClickItem" />
		<!-- 文件传阅 -->
		<view v-if="currentIndex === 0">
			<view style="padding: 10px;"></view>
			<view v-for="(item, idx) in list" :key="idx">
				<view class="card" @click="onclickShoucang(idx)" v-if="(item.typ == 17)">
					<view class="card-title">
						<uni-row>
							<uni-col :span="24">
								<view style="font-weight: bold;">
									<view>{{item.title}}</view>
								</view>
							</uni-col>
						</uni-row>
					</view>
					<view class="card-body">
						<view>{{item.content}}</view>
					</view>
					<view class="card-footer">
						<uni-row>
							<uni-col :span="12">
								<view class="card-footer-left">
									<uni-tag text="文件传阅" size="mini" type="warning" v-if="(item.typ == 17)"></uni-tag>
									<uni-tag text="公文流转" size="mini" type="error" v-if="(item.typ == 18)"></uni-tag>
									<uni-tag text="内部发文" size="mini" type="success" v-if="(item.typ == 23)"></uni-tag>
								</view>
							</uni-col>
							<uni-col :span="12">
								<view class="card-footer-right">{{getFormatedTimeStr(item.createTime)}}</view>
							</uni-col>
						</uni-row>
					</view>

				</view>
				<view style="padding-bottom: 10px;" v-if="(item.typ == 17)"></view>
			</view>
		</view>
		<!-- 公文流转 -->
		<view v-if="currentIndex === 1">
			<view style="padding: 10px;"></view>
			<view v-for="(item, idx) in list" :key="idx">
				<view class="card" @click="onclickShoucang(idx)" v-if="(item.typ == 18)">
					<view class="card-title">
						<uni-row>
							<uni-col :span="24">
								<view style="font-weight: bold;">
									<view>{{item.title}}</view>
								</view>
							</uni-col>
						</uni-row>
					</view>
					<view class="card-body">
						<view>{{item.content}}</view>
					</view>
					<view class="card-footer">
						<uni-row>
							<uni-col :span="12">
								<view class="card-footer-left">
									<uni-tag text="文件传阅" size="mini" type="warning" v-if="(item.typ == 17)"></uni-tag>
									<uni-tag text="公文流转" size="mini" type="error" v-if="(item.typ == 18)"></uni-tag>
									<uni-tag text="内部发文" size="mini" type="success" v-if="(item.typ == 23)"></uni-tag>
								</view>
							</uni-col>
							<uni-col :span="12">
								<view class="card-footer-right">{{getFormatedTimeStr(item.createTime)}}</view>
							</uni-col>
						</uni-row>
					</view>

				</view>
				<view style="padding-bottom: 10px;" v-if="(item.typ == 18)"></view>
			</view>
		</view>
		<!-- 内部发文 -->
		<view v-if="currentIndex === 2">
			<view style="padding: 10px;"></view>
			<view v-for="(item, idx) in list" :key="idx">
				<view class="card" @click="onclickShoucang(idx)" v-if="(item.typ == 23)">
					<view class="card-title">
						<uni-row>
							<uni-col :span="24">
								<view style="font-weight: bold;">
									<view>{{item.title}}</view>
								</view>
							</uni-col>
						</uni-row>
					</view>
					<view class="card-body">
						<view>{{item.content}}</view>
					</view>
					<view class="card-footer">
						<uni-row>
							<uni-col :span="12">
								<view class="card-footer-left">
									<uni-tag text="文件传阅" size="mini" type="warning" v-if="(item.typ == 17)"></uni-tag>
									<uni-tag text="公文流转" size="mini" type="error" v-if="(item.typ == 18)"></uni-tag>
									<uni-tag text="内部发文" size="mini" type="success" v-if="(item.typ == 23)"></uni-tag>
								</view>
							</uni-col>
							<uni-col :span="12">
								<view class="card-footer-right">{{getFormatedTimeStr(item.createTime)}}</view>
							</uni-col>
						</uni-row>
					</view>

				</view>
				<view style="padding-bottom: 10px;" v-if="(item.typ == 23)"></view>
			</view>

		</view>

		<uni-load-more :status="moreStatus"></uni-load-more>
	</view>
</template>

<script>
	import {
		websock
	} from '../../net/websock'
	import moment from 'moment'

	export default {
		data() {
			return {
				list: [],
				moreStatus: 'more',
				items: ['文件传阅', '公文流转', '内部发文'],
				currentIndex: 0
			}
		},
		async onReady() {
			this.getOlderShoucang()
		},
		// 下拉刷新，获取最新的数据
		onPullDownRefresh() {
			console.log('onPullDownRefresh')
			this.getNewestShoucang()
		},
		// 上拉刷新，获取历史数据
		onReachBottom() {
			console.log('onReachBottom')
			this.getOlderShoucang()
		},
		methods: {
			// 切换标签
			onClickItem(e) {
				if (this.currentIndex !== e.currentIndex) {
					this.currentIndex = e.currentIndex
				}
			},
			// 获取最新的通知
			async getNewestShoucang() {
				try {
					if (this.list.length == 0) {
						let res = await websock.getGongwenShoucangList({
							pageCount: 10
						})
						uni.stopPullDownRefresh()
						this.list = [...res, ...this.list]
					} else {
						let res = await websock.getGongwenShoucangList({
							greaterThan: this.list[0].id,
							pageCount: 10
						})
						uni.stopPullDownRefresh()
						this.list = [...res, ...this.list]
					}
				} catch (e) {
					uni.showToast({
						title: e.message
					})
				}
			},
			// 获取老的数据
			async getOlderShoucang() {
				this.moreStatus = 'loading'
				try {
					if (this.list.length == 0) {
						let res = await websock.getGongwenShoucangList({
							pageCount: 10
						})
						this.list = [...this.list, ...res]
						if (res.length < 10) {
							this.moreStatus = 'noMore'
						} else {
							this.moreStatus = 'more'
						}
					} else {
						console.log('before get list not empty')
						let res = await websock.getGongwenShoucangList({
							lessThan: this.list[this.list.length - 1].id,
							pageCount: 10
						})
						console.log('getOlderShoucang list not empty', res)
						this.list = [...this.list, ...res]
						if (res.length < 10) {
							this.moreStatus = 'noMore'
						} else {
							this.moreStatus = 'more'
						}
					}
				} catch (e) {
					uni.showToast({
						title: e.message
					})
				}
			},
			getFormatedTimeStr(stamp, str = 'MM-DD HH:mm:ss') {
				return moment(stamp).format(str)
			},
			onclickShoucang(idx) {
				if (this.list[idx].typ == 17) {
					uni.navigateTo({
						url: `/pages/task/chuanyueWenjianDetail?id=${this.list[idx].relevenceId}&taskid=${this.list[idx].id}`
					})
				} else if (this.list[idx].typ == 18) {
					uni.navigateTo({
						url: `/pages/task/gongwenLiuzhuanDetail?id=${this.list[idx].relevenceId}&taskid=${this.list[idx].id}`
					})
				} else if (this.list[idx].typ == 23) {
					uni.navigateTo({
						url: `/pages/task/neibuFawenDetail?id=${this.list[idx].relevenceId}&taskid=${this.list[idx].id}`
					})
				}
			}
		}
	}
</script>

<style>
	@import url("../common/common.css")
</style>
